import { _decorator, Camera, Component, EventTouch, find, Node, v3, Vec3 } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import AllyApi from '../api/AllyApi';
import { Ally, AllyOperateType } from '../entity/Ally';
import { AllyUser } from '../entity/AllyUser';
import { EventManager } from '../event/EventManager';
import { UserInfo } from '../common/UserInfo';
import { RankDetail } from '../rank/RankDetail';
const { ccclass, property } = _decorator;

@ccclass('MemberOper')
export class MemberOper extends Component {
    @property(Node)
    mask: Node
    @property(Node)
    center: Node
    @property(Node)
    look: Node
    @property(Node)
    kickout: Node

    private user: AllyUser
    private ally:Ally

    start() {

        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.look.on(Node.EventType.TOUCH_END, this.onLook, this)
        this.kickout.on(Node.EventType.TOUCH_END, this.onKickout, this)
    }

    setData(e: EventTouch, user: AllyUser, ally:Ally) {
        this.user = user
        this.ally = ally

        let uicamera = find('Canvas/UICamera').getComponent(Camera)
        // let worldPos = node.worldPosition
        // this.center.position = uicamera.convertToUINode(worldPos, this.node)

        // let location = e.getUILocation()
        // this.center.position = v3(location.x, location.y, 0)

        let worldPos = uicamera.screenToWorld(v3(e.getLocationX(), e.getLocationY(), 0))
        this.center.position = uicamera.convertToUINode(worldPos, this.node)

        //判断我是不是盟主
        if(this.ally.isLeader){
            // 判断是否点到了盟主自己
            if(this.user.UserId == UserInfo.instance.villageData.uid){
                this.kickout.active = false
            }
        } else {
            this.kickout.active = false
        }
    }

    async onLook() {
        let ui = await UIManager.instance.openUI(PrefabEnum.RankDetail)
        ui.getComponent(RankDetail).setData(this.user.UserId)
        ui.getComponent(RankDetail).kickout.active = true
        this.onClose()
    }

    async onKickout() {

        try {
            await UIManager.instance.modal('提示', `您确定将${this.user.UserName}踢出联盟吗？`,)
            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.Kickout,
                uid: this.user.UserId
            })
            UIManager.instance.closeLoading()
            console.log(res)
            EventManager.instance.emit(EventManager.EventType.KickoutAlly, this.user)

            this.onClose()
        } catch (error) {

        }


    }

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.MemberOper)
    }


}


